/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.filter.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Collection;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.genia.toolbox.basics.manager.StreamManager;
import com.genia.toolbox.constants.client.Charset;
import com.genia.toolbox.web.io.MultiQueryableHttpServletRequest;
import com.genia.toolbox.web.spring.multipart.RequestUploadListener;

/**
 * This filter wrapps the request in a
 * <code>MultiQueryableHttpServletRequest</code>.
 */
public class MultiQueryableFilter
    extends AbstractCustomFilter
{

  /**
   * the <code>Log</code> variabe.
   */
  private static final Logger LOGGER = LoggerFactory.getLogger(MultiQueryableFilter.class);

  /**
   * the {@link Collection} of {@link RequestUploadListener} to delegate to.
   */
  private Collection<RequestUploadListener> requestUploadListeners;

  /**
   * the <code>StreamManager</code> to use.
   */
  private StreamManager streamManager;



  /**
   * Causes the next filter in the chain to be invoked with a request wrapped in
   * a <code>MultiQueryableHttpServletRequest</code>.
   * 
   * @param request
   *          the request to pass along the chain
   * @param response
   *          the response to pass along the chain
   * @param chain
   *          the next filter in the chain
   * @throws IOException
   *           when an error occurred
   * @throws ServletException
   *           when an error occurred
   * @see javax.servlet.FilterChain#doFilter(javax.servlet.ServletRequest,
   *      javax.servlet.ServletResponse)
   */
  public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
      throws IOException, ServletException
  {
    ServletRequest newRequest = request;
    if (request instanceof HttpServletRequest) {
      if (request.getCharacterEncoding() == null) {
        try {
          request.setCharacterEncoding(Charset.UTF8);
        }
        catch (UnsupportedEncodingException e) {
          // Can't happen.
          LOGGER.error(e.getMessage(), e);
        }
      }
      newRequest = new MultiQueryableHttpServletRequest(getStreamManager(), (HttpServletRequest) request, getRequestUploadListeners());
    }
    chain.doFilter(newRequest, response);
  }



  /**
   * getter for the order property. This filter must be the first one.
   * 
   * @return the order
   */
  @Override
  public int getOrder()
  {
    return Integer.MIN_VALUE;
  }



  /**
   * getter for the requestUploadListeners property.
   * 
   * @return the requestUploadListeners
   */
  public Collection<RequestUploadListener> getRequestUploadListeners()
  {
    return requestUploadListeners;
  }



  /**
   * getter for the streamManager property.
   * 
   * @return the streamManager
   */
  public StreamManager getStreamManager()
  {
    return streamManager;
  }



  /**
   * setter for the requestUploadListeners property.
   * 
   * @param requestUploadListeners
   *          the requestUploadListeners to set
   */
  public void setRequestUploadListeners(Collection<RequestUploadListener> requestUploadListeners)
  {
    this.requestUploadListeners = requestUploadListeners;
  }



  /**
   * setter for the streamManager property.
   * 
   * @param streamManager
   *          the streamManager to set
   */
  public void setStreamManager(final StreamManager streamManager)
  {
    this.streamManager = streamManager;
  }

}
